<template>
  <div class="app-container">
    <el-form label-position="right" label-width="120px" :model="readerForm" :rules="rules">
     
      <el-form-item label="数据库源：" prop="sourceId">
        <el-select v-model="readerForm.sourceId" filterable  @change="rDsChange">
          <el-option
            v-for="item in rDsList"
            :key="item.id"
            :label="item.sourceName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
  
      <el-form-item label="切分字段：" prop="splitPk">
        <el-input v-model="readerForm.splitPk" placeholder="切分主键"  />
      </el-form-item>
      <el-form-item label="数据库表名：">
        <el-checkbox
          v-model="readerForm.checkAll"
          :indeterminate="readerForm.isIndeterminate"
          @change="rHandleCheckAllChange"
        >全选
        </el-checkbox>
        <div style="margin: 15px 0;" />
        <el-checkbox-group v-model="readerForm.tables" @change="rHandleCheckedChange">
          <el-checkbox v-for="c in rTbList" :key="c.id" :label="c">{{ c.tableName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import * as dsQueryApi from '@/api/dts/metadata-query'
// import { list as jdbcDsList } from '@/api/dts/datax-jdbcDatasource'
import Bus from '../busReader'
import { listDataSource } from '@/api/metadata/sqlconsole'
import { getTableList } from '@/api/masterdata/masterdataTable'
export default {
  name: 'TableReader',
  props:['reader','readidList'],
  data() {
    return {
      rDsList: [],
      rTbList: [],
      schemaList: [],
      loading: false,
      active: 1,
      dataSource: '',
      readerForm: {
        sourceId: undefined,
        sourceName:undefined,
        tables: [],
        checkAll: false,
        isIndeterminate: true,
        splitPk: '',
        tableSchema: ''
      },
      rules: {
        sourceId: [{ required: true, message: '数据库源为必填项', trigger: 'change' }]
      }
    }
  },

  created() {
    this.getJdbcDs()
  },
  methods: {
    getHistory(){
      this.readerForm.sourceId=this.reader.sourceId
      this.readerForm.splitPk=this.reader.splitPk
      this.getTables()
    },
    rest(){
      this.readerForm= {
        sourceId: undefined,
        sourceName:undefined,
        tables: [],
        checkAll: false,
        splitPk:''
      }
      this.rTbList=[]
    },
    // 获取可用数据源
    getJdbcDs() {
      this.loading = true
      listDataSource().then(response => {
        if (response.data.code == 200) {
        this.rDsList =  response.data.data
        this.loading = false
        }
      })
    },
    // 获取表名
    getTables(type) {
        // 组装
        getTableList({ sourceId: this.readerForm.sourceId }).then(res => {
                if (res.data.code === 200) {
                  this.rTbList = res.data.data
                  if(this.readidList.length>0){
                    let arr=[]
                    res.data.data.forEach(item=>{
                      this.readidList.forEach(it=>{
                        if(item.id==it){
                         arr.push(item)
                        }
                      })
                      
                    })
                    this.readerForm.tables=arr
                  }
                }
            })
    },
    getSchema() {
      const obj = {
        sourceId: this.readerForm.sourceId
      }
      getTableList(obj).then(res => {
                if (res.data.code === 200) {
                  this.schemaList = res.data.data
                }
            })
    },
    // schema 切换
    schemaChange(e) {
      this.readerForm.tableSchema = e
      // 获取可用表
      this.getTables('reader')
    },
    // reader 数据源切换
    rDsChange(e) {
      // 清空
      this.readerForm.tables=[]//选中表清空
      this.readerForm.tableName = ''
      this.readerForm.sourceId = e
      this.rDsList.find((item) => {
        if (item.id === e) {
          this.readerForm.sourceName=item.sourceName
          this.dataSource = item
        }
      })
      Bus.sourceId = e
      this.$emit('selectDataSource', this.dataSource)
      // 获取可用表
      this.getTables('reader')
    },
    rHandleCheckAllChange(val) {
      this.readerForm.tables = val ? this.rTbList : []
      this.readerForm.isIndeterminate = false
    },
    rHandleCheckedChange(value) {
      console.log(value)
      const checkedCount = value.length
      this.readerForm.checkAll = checkedCount === this.rTbList.length
      this.readerForm.isIndeterminate = checkedCount > 0 && checkedCount < this.rTbList.length
    },
    getData() {
      // if (Bus.sourceId) {
      //   this.readerForm.sourceId = Bus.sourceId
      // }
      return this.readerForm
    }
  }
}
</script>
